package com.pimee.web.sys;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysConfig;
import com.pimee.service.admin.ISysConfigService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

/**
 * 参数配置 信息操作处理
 *
 * @author bruce shaw
 */
@Controller
@RequestMapping("/admin/sys/config")
public class SysConfigController extends AbstractController {
    private String prefix = "admin/sys/config";

    @Autowired
    private ISysConfigService sysConfigService;

    @ApiOperation("查询参数配置分页列表")
    @RequiresPermissions("system:config:list")
    @PostMapping("/list")
    @ResponseBody
    public Object pageList(@RequestParam Map<String, Object> params, ModelMap modelMap) {
    	PageInfo<SysConfig> pageList = sysConfigService.pageList(params);
		return setSuccessModelMap(modelMap, pageList);
    }

    @ApiOperation("进入新增参数配置")
    @GetMapping("/toAdd")
    public String add() {
        return prefix + "/add";
    }

    @ApiOperation("进入新增参数配置页")
    @RequiresPermissions("system:config:add")
    @PostMapping("/add")
    @ResponseBody
    public Object addSave(@Validated SysConfig param, ModelMap modelMap) {
    	String loginName = ShiroUtils.getLoginName();
    	param.setCreateBy(loginName);
    	param.setUpdateBy(loginName);
    	sysConfigService.insertConfig(param);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation("进入修改参数配置页")
    @GetMapping("/toEdit/{configId}")
    public String edit(@PathVariable("configId") Long configId, ModelMap modelMap) {
    	modelMap.put("config", sysConfigService.selectByKey(configId));
        return prefix + "/edit";
    }

    @ApiOperation("修改保存参数配置")
    @RequiresPermissions("system:config:edit")
    @PostMapping("/edit")
    @ResponseBody
    public Object editSave(@Validated SysConfig param, ModelMap modelMap) {
    	param.setUpdateBy(ShiroUtils.getLoginName());
        sysConfigService.updateConfig(param);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation("删除参数配置")
    @RequiresPermissions("system:config:remove")
    @PostMapping("/remove/{configId}")
    @ResponseBody
    public Object remove(@PathVariable("configId") Long configId, ModelMap modelMap) {
    	sysConfigService.deleteByKey(configId);
        return setSuccessModelMap(modelMap);
    }
    
}
